﻿package jokers{
	import jokers.*;
	import flash.utils.*;

    public class JTimer {
		private static var jt:JTimer;
		
		private var timeDes:int;//Time 间隔
		private var timePass:uint = 0;//经过的时间
		private var id:uint;
		private var func:Function;
		private var isTimeStop:Boolean = false;
		
		private var mtimePause:Boolean = false;
		
		private var timeCombo:uint = 0;
		
		private var mdelay:int = 0;
		
		public function JTimer(timeDes:int = 100){
			this.timeDes = timeDes;
			
			id = setInterval(timeTick,timeDes);
			
			jt = this;
		}
		
		public static function getInstance():JTimer{
			return jt;
		}
		
		public function timeStop():void{
			clearInterval(id);
			this.isTimeStop = true;
		}
		
		public function timePause():void{
			mtimePause = true;
		}
		
		public function timePlay():void{
			if(mtimePause){
				mtimePause = false;
			}
		}
		
		public function isStop():Boolean{
			return this.isTimeStop;
		}
		
		public function setTimerFunc(func:Function):void{
			this.func = func;
		}
		
		public function fouseTick():void{
			timeTick();
		}
		
		public function isCombo(ms:int = 2000):Boolean{
			var ret:Boolean = false;
			
			if(timePass - timeCombo <= ms/timeDes && timeCombo != 0){
				ret = true;
			}
			timeCombo = timePass;
			
			return ret;
		}
		
		public function refreshComboTime():void{
			timeCombo = timePass;
		}
		
		public function delay():void{
			mdelay = 1;
		}
		
		public function getTimePass():int{
			return timePass * timeDes / 1000;
		}
		
		private function timeTick():void{
			if(mtimePause) return;
			
			timePass++;
			
			JPara.timer = JPara.timer_total - (timePass * timeDes ) / 1000;
			
			if(mdelay > 0){
				mdelay--;
				return;
			}
			
			if(func != null) func();
		}
	}
}